package org.jeecg.modules.controller;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.modules.wx.entity.WxUserinfo;
import org.jeecg.common.modules.wx.service.impl.WxUserinfoServiceImpl;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.result.Result;
import org.jeecg.common.util.result.ResultMapper;
import org.jeecg.modules.data.Common;
import org.jeecg.modules.service.ApiVideoService;
import org.jeecg.modules.service.ApiWxUserInfoService;
import org.jeecg.modules.vo.request.RefreshLoginGroupIdRequestVo;
import org.jeecg.modules.vo.request.StartMCUMixTranscodeRequestVo;
import org.jeecg.modules.vo.respone.IMUserVo;
import org.springframework.cache.support.NullValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * @author guoyh
 * @ClassName IMController
 * @Description
 * @since 2020/8/29 18:28
 **/
@Slf4j
@RestController
public class IMController extends BaseController {

    @Resource
    private ApiWxUserInfoService apiWxUserInfoService;

    @Resource
    private WxUserinfoServiceImpl wxUserinfoService;
    @Resource
    private ApiVideoService apiVideoService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 刷新im 签名
     *
     * @return
     */
    @GetMapping("api/v1/refreshSign")
    public Result<IMUserVo> refreshSign(HttpServletRequest req, HttpServletResponse response) throws IOException {
        log.info("api/v1/refreshSign");
        //token不存在，重新授权
        if (Objects.isNull(req.getHeader("X-Access-Token"))) {
            if (CommonUtils.IS_ZS) {
                response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4f53ddd052e1798c&redirect_uri=https%3a%2f%2fzzt.weixin.api.dyzh120.com%2fauth%3furl%3dhttps%3a%2f%2fzzt.weixin.dyzh120.com%2fdoctors%3ftoken%3d&response_type=code&scope=snsapi_userinfo&state=DEFAULT&connect_redirect=1#wechat_redirect");
            }
            return null;
        }
        String wxUserInfoId = null;
//        try{
//            if(Objects.nonNull(getWxUserinfoId())){ wxUserInfoId = getWxUserinfoId(); }
//        } catch (Exception e){
//            log.error("getOpenId()获取信息失败",e);
//        }
        if (Objects.isNull(wxUserInfoId)) {
            String openid = (String) redisUtil.get(req.getHeader("X-Access-Token"));
            WxUserinfo wxUserinfo = wxUserinfoService.getByOpenid(openid);
            wxUserInfoId = wxUserinfo.getId();
        }

        String sign = apiWxUserInfoService.refreshSign(wxUserInfoId);
        IMUserVo userVo = new IMUserVo();
        userVo.setUserId(wxUserInfoId);
        userVo.setUserSign(sign);
        return ResultMapper.<IMUserVo>ok().setData(userVo);
    }

    @PostMapping("api/v1/refreshLoginGroupId")
    public Result<NullValue> refreshLoginGroupId(@RequestBody RefreshLoginGroupIdRequestVo requestVo,
                                                 HttpServletRequest req, HttpServletResponse response) throws IOException {
        log.info("api/v1/refreshLoginGroupId");
        //token不存在，重新授权
        if (Objects.isNull(req.getHeader("X-Access-Token"))) {
            if (CommonUtils.IS_ZS) {
                 //测试
                response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4f53ddd052e1798c&redirect_uri=https%3a%2f%2fzzt.weixin.api.dyzh120.com%2fauth%3furl%3dhttps%3a%2f%2fzzt.weixin.dyzh120.com%2fdoctors%3ftoken%3d&response_type=code&scope=snsapi_userinfo&state=DEFAULT&connect_redirect=1#wechat_redirect");
            }

            log.info("api/v1/refreshLoginGroupId");

            return null;
        }
        String openId = null;
        try {
            if (Objects.nonNull(getOpenId())) {
                openId = getOpenId();
            }
        } catch (Exception e) {
            log.error("getOpenId()获取信息失败", e);
        }
        if (Objects.isNull(openId)) {
            openId = (String) redisUtil.get(req.getHeader("X-Access-Token"));
        }

        WxUserinfo wxUserinfo = wxUserinfoService.getByOpenid(openId);
        if (Objects.isNull(wxUserinfo)) {
            return ResultMapper.error();
        }
        wxUserinfo.setLastConnTime(new Date());
        wxUserinfo.setLoginGroupId(requestVo.getGroupId());
        wxUserinfoService.updateById(wxUserinfo);
        return ResultMapper.ok();
    }


    @PostMapping("api/v1/startMCUMixTranscode")
    public Result<NullValue> startMCUMixTranscode(@RequestBody StartMCUMixTranscodeRequestVo request) {
        return apiVideoService.startMCUMixTranscode(request);
    }
//    @GetMapping()
//    public Result info(@RequestParam String doctorId, String token){
//        ApiJwtVo businessInfo = ApiJwtUtil.getBusinessInfo(token);
//        String wxUserId =businessInfo.getBusinessId();
//        int hasOrder =apiOrderService.order(doctorId,wxUserId).size();
//        wxUserinfoService.getById(wxUserId);
//        DoctorWxUserinfoRelation rela =doctorWxUserinfoRelationService.getByDoctorAndOpenid(doctorId,wxUserinfoService.getById(wxUserId).getOpenid());
//        rela.getTextNumber();
//
//
//
//
//    }
}
